package test_package;

import junit.framework.Assert;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 27.11.13
 * Time: 12:17
 * To change this template use File | Settings | File Templates.
 */
public class buxfer_com_AccountMainPage {
    private WebDriver driver;

    private  String verification_data [][] = {                  //menu item, search keywords, xpath
            {"Profile", "destinygenerated@gmail.com","/html/body/div[2]/div[2]/div[5]/div[2]/div[2]/div[5]/form/div/div[2]/table/tbody/tr/td"},
            {"Password", "Current password", "//div[contains(text(),'Current password')]"},
            {"Membership", "Membership plan","//div[contains(text(),'Membership plan')]"},
            {"Alerts", "Alert Type","/html/body/div[2]/div[2]/div[5]/div[6]/div[2]/div[5]/form/div/table/tbody/tr/th"},
            {"Currency", "Display format","/html/body/div[2]/div[2]/div[5]/div[7]/div[2]/div[5]/form/div[5]"},
            {"Help us translate", "Translate","/html/body/div[2]/div[2]/div[5]/div[8]/div/div/div[2]"},
            {"Automatic rules", "Add Tag Rule","/html/body/div[2]/div[2]/div[5]/div[9]/div[2]/div[5]/div/div[2]/table/tbody/tr[2]/td/div/a"},
            {"Download transaction", "Select the transactions you want to download:","/html/body/div[2]/div[2]/div[5]/div[10]/div[2]/div[3]/div/div"},
            {"Logout", "Login", "//a[contains(text(),'Login')]"}};


    By ProfileMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Profile')]");
    By ProfileElement = By.xpath("/html/body/div[2]/div[2]/div[5]/div[2]/div[2]/div[5]/form/div/div[2]/table/tbody/tr/td[contains(text(),'destinygenerated@gmail.com')]");

    By PasswordMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Password')]");
    By PasswordElement = By.xpath("//div[contains(text(),'Current password')]");

    By MembershipMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Membership')]");
    By MembershipElement = By.xpath("//a[contains(text(),'Invite friends to get a FREE upgrade')]");

    By AlertsMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Alerts')]");
    By AlertsElement = By.xpath("/html/body/div[2]/div[2]/div[5]/div[5]/div[2]/div[5]/form/div[2]/div[2]/div/div[8][contains(text(),'destinygenerated@gmail.com')]");

    By CurrencyMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Currency')]");
    By CurrencyElement = By.xpath("//*[@id=\"pref_currencyId\"]");

    By TranslateMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Help us translate')]");
    By TranslateElement = By.xpath("//div[contains(text(),'Translate')]");

    By RulesMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Automatic rules')]");
    By RulesElement = By.xpath("//a[contains(text(),'Add Tag Rule')]");

    By DownloadMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Download transactions')]");
    By DownloadElement = By.xpath("//a[contains(text(),'Download')]");

    By LogoutMenuItem=By.xpath("//div[@class='lAlign']//div[contains(text(),'Logout')]");
    By LogoutElement= By.xpath("//a[contains(text(),'Login')]");

    private By closePopUpBtn= By.xpath("//*[@id=\"overlay\"]/table/tbody/tr[3]/td/div[1]/div/img");
    private By menuFirstLevel = By.xpath("(//div[@id='__pageBody']//div[@class='ttMargin'])[1]//div[contains(text(),'Account')][@class='drpdwnHandler']");

    public buxfer_com_AccountMainPage(WebDriver driver){
        this.driver=driver;
    }

    public buxfer_com_AccountMainPage closePopUp() {
        driver.findElement(closePopUpBtn).click();
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertProfileMenuItem() {
        click_on_menu_item(ProfileMenuItem);
        Assert.assertTrue(driver.findElement(ProfileElement).isDisplayed());
        takeScreenshot("assertProfileMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertPasswordMenuItem() {
        click_on_menu_item(PasswordMenuItem);
        Assert.assertTrue(driver.findElement(PasswordElement).isDisplayed());
        takeScreenshot("assertPasswordMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertMembershipMenuItem() {
        click_on_menu_item(MembershipMenuItem);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        Assert.assertTrue(driver.findElement(MembershipElement).isDisplayed());
        takeScreenshot("assertMembershipMenuItem");

        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertAlertsMenuItem() {
        click_on_menu_item(AlertsMenuItem);
        Assert.assertTrue(driver.findElement(AlertsElement).isDisplayed());
        takeScreenshot("assertAlertsMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertCurrencyMenuItem() {
        click_on_menu_item(CurrencyMenuItem);
        Assert.assertTrue(driver.findElement(CurrencyElement).isDisplayed());
        takeScreenshot("assertCurrencyMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertTranslateMenuItem() {
        click_on_menu_item(TranslateMenuItem);
        Assert.assertTrue(driver.findElement(TranslateElement).isDisplayed());
        takeScreenshot("assertTranslateMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertRulesMenuItem() {
        click_on_menu_item(RulesMenuItem);
        Assert.assertTrue(driver.findElement(RulesElement).isDisplayed());
        takeScreenshot("assertRulesMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertDownloadMenuItem() {
        click_on_menu_item(DownloadMenuItem);
        Assert.assertTrue(driver.findElement(DownloadElement).isDisplayed());
        takeScreenshot("assertDownloadMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public buxfer_com_AccountMainPage assertLogoutMenuItem() {
        click_on_menu_item(LogoutMenuItem);
        Assert.assertTrue(driver.findElement(LogoutElement).isDisplayed());
        takeScreenshot("assertLogoutMenuItem");
        return new buxfer_com_AccountMainPage(driver);
    }

    public void click_on_menu_item(By item){
        //driver.findElement(menuFirstLevel).click();
        new Actions(driver).moveToElement(driver.findElement(menuFirstLevel)).click().perform();
        //driver.findElement(item).click();
        new Actions(driver).moveToElement(driver.findElement(item)).click().perform();
                try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.tagName("a"));
    }

    private void takeScreenshot(String prefix)  {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmm").format(Calendar.getInstance().getTime());
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(scrFile, new File("c:\\"+ timeStamp + prefix +"_screenshot.png"));
        } catch (IOException e) {
            System.out.println("Failed to capture screenshot: " + e.getMessage());
        }

    }
}
